package com.company;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

//import org.apache.log4j.Logger;

/**
 *
 *
 */
public class Similarity {
  //  static Logger logger = Logger.getLogger(Similarity.class.getName());
    Map<String, Double> rating_map = new HashMap<String, Double>();
    List<Double> rating_map_list = new ArrayList<Double>();


    /**
     * @param args
     */
    public static void main(String[] args) {
        Similarity similarity1 = new Similarity();
        similarity1.rating_map_list.add(20d);
        similarity1.rating_map_list.add(7d);
        similarity1.rating_map_list.add(26d);
        Similarity similarity2 = new Similarity();
        similarity2.rating_map_list.add(7d);
        similarity2.rating_map_list.add(3d);
        similarity2.rating_map_list.add(6d);
        System.out.println(similarity1.getsimilarity_bydim(similarity2));
     //   logger.info("" + similarity1.getsimilarity_bydim(similarity2)); //0.8多,属于高度相关


        Similarity similarity3 = new Similarity();
        similarity3.rating_map_list.add(12d);
        similarity3.rating_map_list.add(4d);
        similarity3.rating_map_list.add(8d);
        Similarity similarity4 = new Similarity();
        similarity4.rating_map_list.add(3d);
        similarity4.rating_map_list.add(1d);
        similarity4.rating_map_list.add(2d);
        System.out.println(similarity3.getsimilarity_bydim(similarity4));
    //    logger.info("" + similarity3.getsimilarity_bydim(similarity4)); //结果是1.0成比例其实就是前面和后面是倍数关系

    }


    public Double getsimilarity_bydim(Similarity u) {
        if(this.rating_map_list.size()!=u.rating_map_list.size()){
            return null;
        }
        double sim = 0d; //最后的皮尔逊相关度系数
        double common_items_len = this.rating_map_list.size(); //操作数的个数
        double this_sum = 0d; //第一个相关数的和
        double u_sum = 0d; //第二个相关数的和
        double this_sum_sq = 0d; //第一个相关数的平方和
        double u_sum_sq = 0d; //第二个相关数的平方和
        double p_sum = 0d; //两个相关数乘积的和

        for(int i = 0;i<this.rating_map_list.size();i++){
            double this_grade = this.rating_map_list.get(i);
            double u_grade = u.rating_map_list.get(i);
            //评分求和                     //平方和                     //乘积和
            this_sum += this_grade;
            u_sum += u_grade;
            this_sum_sq += Math.pow(this_grade, 2);
            u_sum_sq += Math.pow(u_grade, 2);
            p_sum += this_grade*u_grade;
        }

    //    logger.info("common_items_len:"+common_items_len);
     //   logger.info("p_sum:"+p_sum);
      //  logger.info("this_sum:"+this_sum);
       // logger.info("u_sum:"+u_sum);
        double num = common_items_len * p_sum - this_sum * u_sum;
        double den = Math.sqrt((common_items_len * this_sum_sq - Math.pow(this_sum, 2)) * (common_items_len * u_sum_sq - Math.pow(u_sum, 2)));
       // logger.info("" + num + ":" + den);
        sim = (den == 0) ? 1 : num / den;

        return sim;
    }

}
